﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* Mortgage to Bank Dialo
	This dialog will show the list of Fields owned by player,
	for the player to choose what to mortgage in order to pay fee or buy another property
*/



package monopoly.GameController.Dialogs {
	import fl.controls.*;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.events.EventDispatcher;
	import flash.text.TextField;
	import fl.containers.ScrollPane;
	import fl.controls.Button;
	import flash.text.TextFieldAutoSize;

	import monopoly.GameController.Dialogs.Templates.*;
	import monopoly.Player.Player;
	import monopoly.Field.Field;
	import monopoly.Localization.*;
	
	public class MortgageDialog extends Sprite
		{
		protected var _chosen_fields:Array;			// fields chosen to be mortgage with bank
		protected var _moneyOwed:Number;
		protected var _stored_data:Object;
		
		protected var _pFields:Array;
		protected var _pFields_cb:Array;
		protected var _sum:Number = 0;
		protected var _chooseLabel:Label = new Label();
		protected var _tf:TextField	= new TextField();
		protected var _OKButton:Button = new Button();
		protected var _mainPane:ScrollPane = new ScrollPane();
		protected var _myStrings:ILocalLanguage = LocalLanguage.getInstance();
		
		protected var VerticalPadding:Number = 20;
		
		
		public function MortgageDialog(playerFields:Array, field:Field)
			{
			_pFields	= playerFields;
			_moneyOwed	= field.Fee;
			_stored_data= field;
			
			setupComponents();
			}
			
		public function get ChosenFields():Array
			{
			return _chosen_fields;
			}
		public function get Sum():Number
			{
			return _sum;
			}
		public function get StoredData():Object
			{
			return _stored_data;
			}
			
		private function setupComponents():void
			{
			var field:Field;
			var cb:CheckBox;
			var last_y:Number = 0;
			
			this.visible = false;
			addChild(_mainPane);
			
			setupSumField();
			setupOKButton();
			
			/* setup main panel */
			
			with (_mainPane)
				{
				setSize(180, 200);
				x = 0;
				y = 0;
				alpha = 0.9;
				//opaqueBackground = GLOBALS.DEFAULT_DIALOG_COLOR;
				}
			
			/* setup chooseLabel */
			
			_mainPane.addChild(_chooseLabel);
			with (_chooseLabel)
				{
				move(5, 5);
				text = _myStrings.DlgMortgageLabel;
				autoSize = TextFieldAutoSize.LEFT;
				}
			
			/* setup check boxes */
			
			for each(field in _pFields)
				{
				cb 			= new CheckBox();
				_mainPane.addChild(cb);
				cb.label	= field.PropertyName;
				cb.y		= cb.y + last_y;
				
				cb.addEventListener(MouseEvent.CLICK, updateFieldList);
				_pFields_cb.push(cb);
				}
			this.visible = true;
			}
		
		private function setupSumField():void 
			{
            addChild(_tf);	// outside the scroll pane!!
			_tf.width = _mainPane.width * 0.25;
            _tf.height = 25;
            _tf.x = _mainPane.width - _tf.width;
            _tf.y = _mainPane.height + VerticalPadding;
            _tf.border = true;
            _tf.text = _myStrings.DlgMortgageText(_moneyOwed);
	        }
		private function setupOKButton():void
			{
			addChild(_OKButton);	// outside the scroll pane!!
			_OKButton.enabled = false;
			_OKButton.addEventListener(MouseEvent.CLICK, OKbuttonClick);
			_OKButton.move(5, _tf.y + _tf.height + VerticalPadding);
			_OKButton.label = "OK";
			}

		private function updateFieldList(e:MouseEvent):void
			{
			var cb:CheckBox = CheckBox(e.target);
			var f_index:int   = 0;
			
			f_index = _pFields_cb.indexOf(cb);
			
			if (cb.selected == true)
				{
				_sum = _sum + _pFields[f_index].Cost;
				_chosen_fields.push(_pFields[f_index]);
				}
			else
				{
				_sum = _sum - _pFields[f_index].Cost;
				_chosen_fields.splice(_chosen_fields.indexOf(_pFields[f_index]), 1);				
				}
            _tf.text = _myStrings.DlgMortgageUpd(_moneyOwed, _sum);
			
			if (_sum >= _moneyOwed)
				_OKButton.enabled = true;
			else
				_OKButton.enabled = false;
			}
		private function OKbuttonClick(e:MouseEvent):void
			{
			/* dialog done */	
			
			this.removeEventListener(MouseEvent.CLICK, OKbuttonClick);
			this.removeEventListener(MouseEvent.CLICK, updateFieldList);
			
			for (var index:int = 0;  index < _mainPane.numChildren; index++)
				{
				_mainPane.removeChildAt(index);
				}
			removeChild(_mainPane);
			removeChild(_tf);
			removeChild(_OKButton);
			
			dispatchEvent(new Event(Event.COMPLETE));
			}
		}
}
	